﻿using System.Net.Http;
using Avalonia.Controls;
using LibVLCSharp.Shared;
using MusicFluent.Models;
using MusicFluent.Views;
using NetEasyMusic.Lib.Models;

namespace MusicFluent.MusicViews;

public partial class VlcView : UserControl
{
    public VlcView(VideoModel item)
    {
        _item = item;
        InitializeComponent();
    }

    private readonly VideoModel _item;
    private readonly LibVLC _vlc = new();

    protected override async void OnInitialized()
    {
        var main = ViewOpera.GetView<MainWindow>(this);
        if (main == null) return;
        main.TaskShow();
        View.MediaPlayer = new MediaPlayer(_vlc);
        TitleBlock.Text = _item.Title;
        DescBlock.Text = _item.Description;
        using var client = new HttpClient();
        var m = await client.GetStreamAsync(_item.Url);
        View.MediaPlayer.Play(new Media(_vlc, new StreamMediaInput(m)));
        main.TaskShow();
    }
}